import request from "@/utils/request";

let controller: AbortController
// 获取购物车信息
export function cartList(userid: string) {
    return request.post<any, Result<CartList[]>>('/cart/list', {
        userid
    })
}

// 添加到购物车
export function cartAdd(userid: string, proid: string, num = 1) {
    return request.post<any, Result<undefined>>('/cart/add', {
        userid,
        proid,
        num
    })
}


// 更新购物车选定状态
export function cartSelectone(cartid: string, flag: boolean) {
    return request.post<any, Result>('/cart/selectone', {
        cartid,
        flag
    })
}

// 更新购物车数量
export function cartUpdatenum(cartid: string, num: number) {
    if (controller) {
        // 1. 尝试取消请求
        controller.abort()
    }
    // 2. 创建新的 AbortController
    controller = new AbortController()
    return request.post<any, Result>('/cart/updatenum', {
        cartid,
        num
    },
        {
            // 3. 传递 signal
            signal: controller.signal
        }
    )
}

// 删除当个数据
export function cartRemove(cartid: string) {
    return request.post<any, Result>('/cart/remove', {
        cartid
    })
}


// 更新所有购物车数据的选中状态
export function cartSelectall(userid: string, type: boolean) {
    return request.post<any, Result>('/cart/selectall', {
        userid,
        type
    })
}


// 清空购物车所有的数据
export function cartRemoveall(userid: string) {
    return request.post<any, Result>('/cart/removeall', {
        userid,
    })
}